package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Log4j2
public class SmsController {
    @Autowired
    private SmsService smsService;

    /*发送短信验证码接口
     * */
    @PostMapping("/login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String, String> param) {
        ErrorResult errorResult;
        //获取请求数据里面的phone数据
        String phone = param.get("phone");
        try {
            //把电话号码传入service层 进行业务处理 获取errorresult
            errorResult = this.smsService.sendChecCode(phone);
            if (null == errorResult) {
                //返回null 表示发送成功
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("发送短信验证失败~ phone= " + phone, e);
            errorResult = ErrorResult.builder().errCode("00002").errMessage("短信验证码发送失败").build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }
}
